package com.android.love.web;

import java.util.ArrayList;
import java.util.List;

public final class Controller {
	private List<CustomWebView> mWebViewList;
	private List<DownloadItem> mDownloadList;
	private static final Controller INSTANCE = new Controller();

	public static Controller getInstance() {
		return INSTANCE;
	}

	/**
	 * 构造函数私有化
	 */
	private Controller() {
		mDownloadList = new ArrayList<DownloadItem>();
	}

	/**
	 * 获取当前webviews List
	 * 
	 * @return
	 */
	public List<CustomWebView> getWebViewList() {
		return mWebViewList;
	}

	/**
	 * 设置当前webview的列表
	 * 
	 * @param list
	 */
	public void setWebViewList(List<CustomWebView> list) {
		mWebViewList = list;
	}

	/**
	 * 获取当前下载列表
	 * 
	 * @return
	 */
	public List<DownloadItem> getDownloadList() {
		return mDownloadList;
	}

	/**
	 * 增加一个下载任务
	 * 
	 */
	public void addToDownload(DownloadItem item) {
		mDownloadList.add(item);
	}

	/**
	 * 清除下载完成
	 */
	public synchronized void clearCompletedDownloads() {
		List<DownloadItem> newList = new ArrayList<DownloadItem>();

		for (DownloadItem item : mDownloadList) {
			if (!item.isFinished()) {
				newList.add(item);
			}
		}

		mDownloadList.clear();
		mDownloadList = newList;
	}

}
